package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Crawlerdetectstatus;
import cn.edu.scau.cmi.ema.domain.Crawlerevent;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("CrawlereventController")
public class CrawlereventController extends CmiEmaController {

	@RequestMapping("/listCrawlerevents")
	public ModelAndView listCrawlerevents(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlereventPaginationService.updatePagination(request, pagination);
		Set<Crawlerevent> crawlerevents = crawlereventPaginationService.getPaginationCrawlerevents(pagination);

		mav.addObject("crawlerevents", crawlerevents);
		mav.addObject("entityName", "crawlerevent");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerevent/listCrawlerevents");
		return mav;
	}

	public String listCrawlerevents() {
		return "redirect:/listCrawlerevents";
	}

	@RequestMapping("/viewCrawlerevent")
	public ModelAndView viewCrawlerevent(@RequestParam Integer crawlereventId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerevent", crawlereventDAO.findCrawlereventByPrimaryKey(crawlereventId));
		mav.setViewName("crawlerevent/viewCrawlerevent");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerevents")
	public ModelAndView confirmDeleteCrawlerevents(
			@RequestParam(value = "deleteCrawlereventsIds", required = true) Set<Integer> deleteCrawlereventsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerevent> deleteCrawlerevents = crawlereventDAO.findCrawlereventsByIds(deleteCrawlereventsIds);
		Set<Crawlerevent> referencedCrawlerevents = crawlereventService.loadReferenceCrawlerevents(deleteCrawlerevents);
		boolean hasReferenced;
		if (referencedCrawlerevents.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerevents);
			hasReferenced = false;
		} else {
			deleteCrawlerevents.removeAll(referencedCrawlerevents);
			mav.addObject("referencedEntitys", referencedCrawlerevents);
			mav.addObject("deleteEntitys", deleteCrawlerevents);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerevent");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerevents");
		return mav;
	}

	@RequestMapping("/deleteCrawlerevent")
	public String deleteCrawlerevent(@RequestParam Integer crawlereventId) {
		Crawlerevent crawlerevent = crawlereventDAO.findCrawlereventByPrimaryKey(crawlereventId);
		crawlereventService.deleteCrawlerevent(crawlerevent);
		return "forward:/listCrawlerevents";
	}

	@RequestMapping("/confirmDeleteCrawlerevent")
	public ModelAndView confirmDeleteCrawlerevent(@RequestParam Integer crawlereventId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerevent", crawlereventDAO.findCrawlereventByPrimaryKey(crawlereventId));

		mav.setViewName("crawlerevent/deleteCrawlerevent");

		return mav;
	}

	@RequestMapping("/deleteCrawlerevents")
	public String deleteCrawlerevents(
			@RequestParam(value = "deleteCrawlereventsId", required = true) Set<Integer> deleteCrawlereventsId) {
		crawlereventService.deleteCrawlerevents(deleteCrawlereventsId);
		return "forward:/listCrawlerevents";
	}

	@RequestMapping("/editCrawlerevent")
	public ModelAndView editCrawlerevent(@RequestParam Integer crawlereventId) {

		ModelAndView mav = new ModelAndView();

		Crawlerevent crawlerevent = crawlereventDAO.findCrawlereventByPrimaryKey(crawlereventId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerevent", crawlerevent);

		// 把所有的引用crawlerevent对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlereventService.loadCrawlereventObjectReferencedSet(crawlerevent));

		// 将编辑页面crawlerevent添加到mav中
		mav.setViewName("crawlerevent/editCrawlerevent");
		return mav;
	}

	@RequestMapping("/newCrawlerevent")
	public ModelAndView newCrawlerevent() {
		ModelAndView mav = new ModelAndView();
		Crawlerevent crawlerevent = new Crawlerevent();

		mav.addObject("crawlerevent", crawlerevent);

		// 把所有的引用crawlerevent对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlereventService.loadCrawlereventObjectReferencedSet(crawlerevent));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerevent/editCrawlerevent");

		return mav;
	}

	@RequestMapping("/saveCrawlerevent")
	public ModelAndView saveCrawlerevent(@ModelAttribute Crawlerevent crawlerevent) {

		ModelAndView mav = new ModelAndView();

		Crawlerdetectstatus crawlerdetectstatusTemp = crawlerevent.getCrawlerdetectstatus();
		Crawlerdetectstatus crawlerdetectstatus = crawlerdetectstatusTemp == null ? null
				: CmiHibernateProxyUtil
						.deproxy(crawlerdetectstatusDAO.findCrawlerdetectstatusById(crawlerdetectstatusTemp.getId()));

		Crawlerevent existingCrawlerevent = CmiHibernateProxyUtil.deproxy(crawlereventDAO
				.findCrawlereventByPhysicalPrimaryKey(crawlerevent.getFilename(), crawlerevent.getRownumber()));

		crawlerevent.setCrawlerdetectstatus(crawlerdetectstatus);
		// 新建记录
		if (crawlerevent.getId() == null) {
			if (crawlerevent.equals(existingCrawlerevent)) {

				mav.addAllObjects(crawlereventService.loadCrawlereventClassReferencedSet(crawlerevent));
				// mav = editContinue(mav, crawlerevent);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerevent/editCrawlerevent");
			} else {
				crawlereventService.saveCrawlerevent(crawlerevent);
				mav.setViewName("forward:/listCrawlerevents");
			}
		}
		// 编辑记录
		else {
			Crawlerevent originCrawlerevent = CmiHibernateProxyUtil
					.deproxy(crawlereventDAO.findCrawlereventByPrimaryKey(crawlerevent.getId()));
			if (!crawlerevent.equals(originCrawlerevent) && crawlerevent.equals(existingCrawlerevent)) {
				mav.addAllObjects(crawlereventService.loadCrawlereventClassReferencedSet(crawlerevent));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerevent/editCrawlerevent");
			} else {
				crawlereventService.saveCrawlerevent(crawlerevent);
				mav.setViewName("forward:/listCrawlerevents");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlereventController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
